package com.frame.platform.dept;

import java.util.List;

import org.hibernate.*;

import com.frame.database.DataBase;
import com.frame.database.DbOperate;
import com.frame.database.JdataBean;
import com.frame.database.util.ConditionMap;
import com.frame.database.util.Seq;
import com.frame.hibernate.HibernateUtil;
import com.frame.platform.user.Wfuserdept;

public class DeptDao {

	public DeptDao(){

	}
	/**
	 * 添加机构
	 * @param dept
	 * @throws HibernateException
	 * @throws Exception
	 */
	public void addDept(Wfdept dept) throws HibernateException, Exception{
//		dept.setDeletetag(new Integer(0));
//		dept.setOrderid(new Integer(999));
//		DataBase.getInstance().save(dept);
		DbOperate db=DbOperate.getInstance();
		JdataBean bean=new JdataBean();
		bean.put("DEPTID", new Seq().getKeyId("Wfdeptid"));
		bean.put("DEPTNAME", dept.getDeptname());
		bean.put("PARDEPTID", dept.getPardeptid());
		bean.put("ORDERID",999 );
		bean.put("DELETETAG",0 );
		db.saveBean("wfdept", bean.getDataMap());
	}
	/**
	 * 修改机构
	 * @param dept
	 */
	public void updateDept(Wfdept dept){
		Session session=HibernateUtil.currentSession();
		Transaction transaction=session.beginTransaction();
		Wfdept olddept=(Wfdept)session.load(Wfdept.class, dept.getDeptid());
		olddept.setDeptname(dept.getDeptname());
		olddept.setIsBoard(dept.getIsBoard());
		session.flush();
		transaction.commit();
		HibernateUtil.closeSession();
	}

	public Wfdept getDept(Integer deptId) throws HibernateException, Exception{
		return (Wfdept)DataBase.getInstance().getBean(Wfdept.class, deptId);
	}
	/**
	 * 删除
	 * @param deptId
	 * @throws Exception
	 */
	public void delDept(Integer deptId) throws Exception{
		DataBase data=DataBase.getInstance();
		data.update("update Wfdept set deletetag=1 where pardeptid=? or deptid=?", new Object[]{deptId,deptId});
	}

	public List getSubDept(String pardept) throws Exception{
		ConditionMap map=new ConditionMap();
		map.put("pardeptid =?").setParameter(new Integer(pardept));
		map.put("deletetag =0");
		map.addOrder("orderid", true);
		return DataBase.getInstance().query("Wfdept", map);
	}

	public void sortDept(String[] deptIds){
		Session session=HibernateUtil.currentSession();
		Transaction transaction=session.beginTransaction();
		for(int i=0;i<deptIds.length;i++){
			Wfdept dept=(Wfdept)session.load(Wfdept.class, new Integer(deptIds[i]));
			dept.setOrderid(new Integer(i+1));
			session.flush();
		}
		transaction.commit();
		HibernateUtil.closeSession();
	}

	public boolean checkDeptName(String dname,Integer deptId,Integer pardeptId) throws Exception{
		if(dname!=null && !"".equals(dname)){
			ConditionMap map=new ConditionMap();
			map.put("deptname = ?").setParameter(dname);
			map.put("pardeptid = ?").setParameter(pardeptId);
			if(deptId!=null)
				map.put("deptid <> ?").setParameter(deptId);
			List list=DataBase.getInstance().query("Wfdept", map);
			if(list.size()>0)
				return false;
			else
				return true;
		}
		return true;
	}

	public String[] getUserDepts(String userId) throws Exception{
		List list=DataBase.getInstance().queryBySql("select d from Wfuserdept d where d.id.userid=?", new Object[]{userId});
		String[] deptids=null;
		if(list.size()>0){
			deptids=new String[list.size()];
			for(int i=0;i<list.size();i++){
				Wfuserdept udept=(Wfuserdept)list.get(i);
				deptids[i]=udept.getId().getDeptid();
			}
		}
		return deptids;
	}

	public String getBoardId(String deptId){
		if(deptId.equals("1"))
			return deptId;
		Session session=HibernateUtil.currentSession();
		Query query=session.createQuery("select d from Wfdept d where d.deptid=:deptid");
		query.setParameter("deptid", new Integer(deptId));
		String bid="";
		if(query.list().size()>0){
			Wfdept dept=(Wfdept)query.list().get(0);
			bid= boardId(dept);
		}
		else
			bid=deptId;

		if(bid.equals("1"))
			if(!deptId.equals("1"))
				bid=deptId;
		HibernateUtil.closeSession();
		return bid;
	}
	public String boardId(Wfdept dept){
		if((dept.getIsBoard()!=null && dept.getIsBoard().intValue()==1) || dept.getPardeptid()==0)
			return dept.getDeptid()+"";
		if(dept.getPdept().getIsBoard()!=null && dept.getPdept().getIsBoard().intValue()==1)
			return dept.getPdept().getDeptid()+"";
		else{
			if(dept.getPdept()!=null)
				return boardId(dept.getPdept());
			else return "";
		}
	}
}
